class Cart{
    constructor(){
        this.$list = $('.cart-item-list')
        this.init();
        this.addEvent();

    }
    init(){
        let cookie_str = $.cookie('products') ? $.cookie('products') : '';
        let cookie_obj = convertStrToObj(cookie_str);
        for(let key in cookie_obj){
            let good = cookie_obj[key];
            this.$list.append(`
            <div class="cart-item check-cart-item" data-good-id="sj1">
                <div class="p-checkbox">
                    <input type="checkbox" name="" id="" checked class="j-checkbox">
                </div>
                <div class="p-goods">
                    <div class="p-img">
                        <img src="${good.src}" alt="">
                    </div>
                    <div class="p-msg">${good.name}</div><br>
                    <p> 颜色：水墨、版本：16GB+1TB、套餐类型：官方标配</p>
                </div>
                <div class="p-price">￥${good.price}</div>
                <div class="p-num">
                    <div class="quantity-form">
                        <a href="javascript:;" class="decrement">-</a>
                        <input type="text" class="itxt" value="${good.num}">
                        <a href="javascript:;" class="increment">+</a>
                    </div>
                </div>
                <div class="p-sum">￥${good.price * good.num}</div>
                <div class="p-action"><a href="javascript:;">删除</a></div>
            </div>

            `)

        }
     
    }
    addEvent(){
        // 加号
        $('.quantity-form .increment').click(function(){
            let id =  $(this).parents('.cart-item').attr('data-good-id');
            let cookie_str = $.cookie('products') ? $.cookie('products') : '';
            let cookie_obj = convertStrToObj(cookie_str);
            // console.log(cookie_obj[id].num  )
            cookie_obj[id].num = parseInt($(this).prev().val()) + 1;
            // let num = cookie_obj[id].num;
            console.log(cookie_obj[id].num)
            // // console.log(num)
            // // cookie_obj[id].num ++;
            // // num ++;
            // console.log(num)
            $.cookie('products',JSON.stringify(cookie_obj),{expires : 7,path : '/'});
            
        })


        // 减号
        $('.quantity-form .decrement').click(function(){
            let id =  $(this).parents('.cart-item').attr('data-good-id');
            let cookie_str = $.cookie('products') ? $.cookie('products') : '';
            let cookie_obj = convertStrToObj(cookie_str);

            cookie_obj[id].num = parseInt($(this).next().val()) - 1;
            console.log($(this).next().val())
            console.log(cookie_obj[id].num)
            // if(cookie_obj[id].num > 1){
            //     cookie_obj[id].num--;
            // }
            $.cookie('products',JSON.stringify(cookie_obj),{expires : 7,path : '/'});
            // $(this).next().val(cookie_obj[id].num);
        })


        // 搜索框
        $('.quantity-form input').blur(function(){
            let id =  $(this).parents('.cart-item').attr('data-good-id');
            let cookie_str = $.cookie('products') ? $.cookie('products') : '';
            let cookie_obj = convertStrToObj(cookie_str);
            let num = parseInt($(this).val());
            if(isNaN(num) || num <= 1){
                num = 1;
                console.log(111)
            }
            console.log(cookie_obj[id].price)
            $(this).val(num);
            $(this).parents('.p-num').next(`￥${num * cookie_obj[id].price}`)
            cookie_obj[id].num = num;
            $.cookie('products',JSON.stringify(cookie_obj),{expires : 7,path : '/'});
        })


        // 删除
        $('.cart-item .cart-item').click(function(){
            let id =  $(this).parents('.cart-item').attr('data-good-id');
            let cookie_str = $.cookie('products') ? $.cookie('products') : '';
            let cookie_obj = convertStrToObj(cookie_str);
            delete cookie_obj[id];
            $.cookie('products',JSON.stringify(cookie_obj),{expires : 7,path : '/'});
            $(this).parent().remove();
        })

    }
}

new Cart();